﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STRobot
{
    public partial class UiClickAddWnd : Form
    {
        public UiClickAddWnd()
        {
            InitializeComponent();
            result = new List<UiClick>();
            show_listbox();
        }

        public UiClickAddWnd(List<UiClick> uiclicks)
        {
            InitializeComponent();
            if (uiclicks == null || uiclicks.Count == 0)
            {
                result = new List<UiClick>();
            }
            else
            {
                result = uiclicks;
            }
            show_listbox();
        }
        public List<UiClick> GetUiclicks()
        {
            if (result.Count == 0)
            {
                return null;
            }
            return result;
        }
        //在当前基上添加多个点击坐标
        private void btn_addclick_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            {
                Point p;
                p.x = textBox3.Text.Length > 0 ? Convert.ToInt32(textBox3.Text) : 0;
                p.y = textBox5.Text.Length > 0 ? Convert.ToInt32(textBox5.Text) : 0;
                if (result[listBox1.SelectedIndex].click == null)
                {
                    result[listBox1.SelectedIndex].click = new List<Point> { p };
                }
                else
                {
                    result[listBox1.SelectedIndex].click.Add(p);
                }
                
            }
            else
            {
                MessageBox.Show("请先添加步骤");
            }
            show_listbox();
        }
        //清空当前选项的所有坐标点
        private void button5_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            {
                result[listBox1.SelectedIndex].click.Clear();
            }
            else
            {
                MessageBox.Show("请选中一条步骤");
            }
            show_listbox();
        }

        //添加多个步骤
        private void button2_Click(object sender, EventArgs e)
        {
            UiClick uiclick = new UiClick();
            if (textBox1.Text.Length > 0)
            {
                uiclick.key = Convert.ToInt32(textBox1.Text);
            }
            if (textBox2.Text.Length > 0)
            {
                uiclick.uiname = textBox2.Text;
            }
            if (textBox4.Text.Length > 0)
            {
                uiclick.npc = textBox4.Text;
            }
            if (textBox3.Text.Length > 0 && textBox5.Text.Length > 0)
            {
                Point p;
                p.x = Convert.ToInt32(textBox3.Text);
                p.y = Convert.ToInt32(textBox5.Text);
                uiclick.click = new List<Point> { p };
            }       
            result.Add(uiclick);
            show_listbox();
            //清空输入框
            textBox1.Clear();
            textBox2.Clear();
            textBox4.Clear();
            textBox5.Clear();
            textBox3.Clear();
        }
        public List<UiClick> result;

        
        //编辑保存选中的项
        private void button4_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            {
                result[listBox1.SelectedIndex].uiname = textBox2.Text;
                result[listBox1.SelectedIndex].key  = textBox1.Text.Length > 0 ? Convert.ToInt32(textBox1.Text) : 0;
                show_listbox();
            }
            else
            {
                MessageBox.Show("请选中一条步骤");
            }
       
        }

        private void show_listbox()
        {
            listBox1.Items.Clear();
            if (result == null)
            {
                return;
            }
            foreach (var item in result)
            {
                listBox1.Items.Add(item.GetString()); 
            }
        }

        //删除选中步骤
        private void button6_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            {
                result.Remove(result[listBox1.SelectedIndex]);
            }
            else
            {
                MessageBox.Show("请选中一条步骤");
            }
            show_listbox();
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listBox1.SelectedIndex > -1)
            {
                var item = result[listBox1.SelectedIndex];
                textBox1.Text = item.key.ToString();
                textBox2.Text = item.uiname;
            }
            else
            {
                MessageBox.Show("请选中一条步骤");
            }
        }

        //保存关闭
        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }
    }
}
